/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 投票活动
	/// module : operationsVote
	/// </summary>
	public interface CNetOperationsVote : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction voteResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<VoteResultData>)wrapper;
                SC_voteResult(session, ref _wrapper.message);
                MessageWrapperPool<VoteResultData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.operationsVote_voteResult, (session, package) => {
                	var result = MessageWrapperPool<VoteResultData>.Get();
                	result.Init(session, voteResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
		public void SC_voteResult(ISession session, ref VoteResultData message) 
		{
            _SC_voteResult(session, ref message);
		}
		
		public void _SC_voteResult(ISession session, ref VoteResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.operationsVote_voteResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.operationsVote_voteResult, message.code);
			MsgAsyncAwaiter<VoteResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
#endregion
#region Server-side Interface
		/// <summary>
        ///  投票
        /// </summary>
    	public UniTask<VoteResultData> CS_vote(int eventId, int voteId)
        {
            return _CS_vote(eventId, voteId);
        }
        
        public UniTask<VoteResultData> _CS_vote(int eventId, int voteId)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(VoteResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.operationsVote_vote, "Send->"+"OperationsVote.CS_vote { "
				+ "eventId="
				+ eventId
				+ ", voteId="
				+ voteId
				+" }");
#endif
        	UniTask<VoteResultData> task = MsgAsyncAwaiter<VoteResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_vote(eventId, voteId));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.operationsVote_voteResult);
			return task;
        }
		
		public static SendPackage Pack_for_vote(int eventId, int voteId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.operationsVote_vote;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(eventId);
			_out.Write_int(voteId);
			return si_p;
		}
#endregion
#region Messages
        public struct VoteResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public int eventId;
	        public int voteId;
	        public List<ItemShowPOD> getItems;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                eventId = _in.HasRemaining() ? _in.Read_int() : default;
                voteId = _in.HasRemaining() ? _in.Read_int() : default;
                getItems = _in.HasRemaining() ? _in.Read_list_pod<ItemShowPOD>(ItemShowPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.operationsVote_voteResult;}
            
            public override string ToString()
            {
                return "OperationsVote.voteResult"+ ", code="+ code+ ", eventId="+ eventId+ ", voteId="+ voteId+ ", getItems="+ getItems.ToString<ItemShowPOD>();
            }
        }
    }
#endregion
}